package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.Impl.ResearchDaoImpl;
import com.ysd.entity.Research;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminResearchServlet extends BaseServlet {
	/***
	 * 获得设计师
	 * localhost:8080/huaduoyoupin/adminResearch?reqName=getResearch
	 */
	public void getResearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResearchDaoImpl rDao=new ResearchDaoImpl();//创建实现类
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		List<Research> rList=rDao.getResearchListPage(page, size);//获得所有设计师
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//创建out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(rList);//将nList转化为json字符串
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
	//分页查询
	//localhost:8080/huaduoyoupin/adminResearch?reqName=getAllResearch
	public void getAllResearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResearchDaoImpl rDao=new ResearchDaoImpl();//创建实现类
		List<Research> rList=rDao.getResearchList();//获得所有设计师
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//创建out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(rList.size());//将nList转化为json字符串
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
	/****
	 * 删除设计师
	 */
	public void delResearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("researchId"));//接收前台传来的参数
		ResearchDaoImpl rDao=new ResearchDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		//调用实现类的删除方法，返回受影响行数
		int n=rDao.delResearch(id);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
	/***
	 * localhost:8080/huaduoyoupin/adminResearch?reqName=addResearch
	 * 添加设计师
	 */
	public void addResearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        ResearchDaoImpl rDao=new ResearchDaoImpl();
		
		String researchName=request.getParameter("researchName");
        String statusName=request.getParameter("statusName");
        String researchImg=request.getParameter("researchImg");
		if(researchName!=null&&!researchName.equals("")) {
			List<Research> rList=rDao.getResearchByName(researchName);
			if(rList.size()==0) {
				Research r=new Research(0, researchName, statusName, researchImg);
				int n=rDao.addResearch(r);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
			}else {
				out.print(Result.toClient(false, "设计师名已存在",""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	//localhost:8080/huaduoyoupin/adminResearch?reqName=upload
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		//1创建SmartUpload对象
		SmartUpload smart = new SmartUpload();
		
		//2设置编码格式
		smart.setCharset("UTF-8");
		
		//3初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//4上传
			smart.upload();
			//5设置上传文件名字
			String fileName = System.currentTimeMillis() + ".jpg";
			//将文件保存到制定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			//响应
			out.print(Result.toClient(true, "上传成功",fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败","图片格式不符"));
		}
		out.flush();
		out.close();
	}
	/**
     * localhost:8080/huaduoyoupin/adminResearch?reqName=getResearchById

	 */
	public void getResearchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        ResearchDaoImpl rDao=new ResearchDaoImpl();
		
		int rId=Integer.parseInt(request.getParameter("researchId"));
		Research r=rDao.getResearchById(rId);
		
		Gson gson=new Gson();
		String res=gson.toJson(r);
		out.print(res);
		
		out.flush();
		out.close();
	}
    /**
     * 修改设计师的内容
     */
	public void updateResearchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        ResearchDaoImpl rDao=new ResearchDaoImpl();
        int researchId=0;
		if(request.getParameter("researchId")!=null && !request.getParameter("researchId").equals("")) {
			researchId=Integer.parseInt(request.getParameter("researchId"));
		}
		String researchName=request.getParameter("researchName");
		String statusName=request.getParameter("statusName");
		String researchImg=request.getParameter("researchImg");
        if(researchName!=null&&!researchName.equals("")) {
        	List<Research> rList=rDao.getResearchByName(researchName);
        	if(rList.size()==0) {
        		Research r=new Research(researchId, researchName, statusName, researchImg);
        		int n=rDao.updateResearchById(r);
        		if(n>0) {
        			out.print(Result.toClient(true, "修改成功",""));
        		}else {
        			out.print(Result.toClient(false, "修改失败",""));
        		}
        	}else {
        		out.print(Result.toClient(false, "设计师名称已存在",""));
        	}
        }
	
		out.flush();
		out.close();
	}
	//批量删除
	//localhost:8080/huaduoyoupin/adminResearch?reqName=delectResearchByIds
	public void delectResearchByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		ResearchDaoImpl rDao=new ResearchDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		String ids=request.getParameter("ids");
		int n=rDao.delectProductByIds(ids);
		//调用实现类的删除方法，返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
}
